<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactTrack\Service;

use Hyperf\Database\Model\Builder;
use MoChat\Framework\Service\AbstractService;
use MoChat\Plugin\ContactTrack\Contract\ContactTrackContract;
use MoChat\Plugin\ContactTrack\Model\ContactTrack;

class ContactTrackService extends AbstractService implements ContactTrackContract
{
    /**
     * @var ContactTrack
     */
    protected $model;

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactTrackById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 查询多条 - 根据ID.
     * @param array $ids ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactTracksById(array $ids, array $columns = ['*']): array
    {
        return $this->model->getAllById($ids, $columns);
    }

    /**
     * 多条分页.
     * @param array $where 查询条件
     * @param array|string[] $columns 查询字段
     * @param array $options 可选项 ['orderByRaw'=> 'id asc', 'perPage' => 15, 'page' => null, 'pageName' => 'page']
     * @return array 分页结果 Hyperf\Paginator\Paginator::toArray
     */
    public function getContactTrackList(array $where, array $columns = ['*'], array $options = []): array
    {
        return $this->model->getPageList($where, $columns, $options);
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createContactTrack(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 添加多条
     * @param array $data 添加的数据
     * @return bool 执行结果
     */
    public function createContactTracks(array $data): bool
    {
        return $this->model->createAll($data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateContactTrackById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 删除 - 单条
     * @param int $id 删除ID
     * @return int 删除条数
     */
    public function deleteContactTrack(int $id): int
    {
        return $this->model->deleteOne($id);
    }

    /**
     * 删除 - 多条
     * @param array $ids 删除ID
     * @return int 删除条数
     */
    public function deleteContactTracks(array $ids): int
    {
        return $this->model->deleteAll($ids);
    }

    /**
     * 查询多条 - 根据客户ID.
     * @param int $contactId 客户ID
     * @param array|string[] $columns 查询字段
     * @param array $orderBy 排序方式
     * @return array 数组
     */
    public function getContactTracksByContactId(int $contactId, array $columns = ['*'], array $orderBy = [['contact_track.created_at', 'desc']]): array
    {
        $res = $this->model::query()
            ->where('contact_id', $contactId);
        if (! empty($orderBy) && is_array($orderBy)) {
            foreach ($orderBy as $v) {
                $res->orderBy($v[0], $v[1]);
            }
        }

        $res = $res->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据客户ID.
     * @param int $contactId 客户ID
     * @param int $event 事件id
     * @param array|string[] $columns 查询字段
     * @param array $orderBy 排序方式
     * @return array 数组
     */
    public function getContactTracksByContactIdEvent(int $contactId, int $event = 0, array $columns = ['*'], array $orderBy = [['contact_track.created_at', 'desc']]): array
    {
        $res = $this->model::query()
            ->where('contact_id', $contactId)
            ->when($event > 0, function (Builder $query) use ($event) {
                return $query->where('event', $event);
            });
        if (! empty($orderBy) && is_array($orderBy)) {
            foreach ($orderBy as $v) {
                $res->orderBy($v[0], $v[1]);
            }
        }

        $res = $res->get($columns);

        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据ID.
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactTrackBySearch(array $where, array $columns = ['*'], array $orderBy = [['contact_track.created_at', 'desc']]): array
    {
        $model = $this->model::query();

        $model = $model->where('corp_id', $where['corpId']);
        if (! empty($where['startTime'])) {
            $model = $model->where('created_at', '>=', $where['startTime']);
        }
        if (! empty($where['endTime'])) {
            $model = $model->where('created_at', '<=', $where['endTime']);
        }
        if (! empty($where['event'])) {
            $model->where('event', $where['event']);
        }
        if (! empty($where['employeeIds'])) {
            $model->whereIn('employee_id', $where['employeeIds']);
        }
        if (! empty($orderBy) && is_array($orderBy)) {
            foreach ($orderBy as $v) {
                $model->orderBy($v[0], $v[1]);
            }
        }

        $res = $model->paginate($where['perPage'], $columns, 'page', $where['page']);
        $res || $res = collect([]);

        if ($res === null) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据客户ID.
     * @param int $contactId 客户ID
     * @param int $event 事件id
     * @param array|string[] $columns 查询字段
     * @param array $orderBy 排序方式
     * @return array 数组
     */
    public function getContactTracksByContactIdEmployeeIdEvent(int $contactId, int $employeeId, string $startTime, string $endTime, int $event = 0, array $columns = ['*'], array $orderBy = [['contact_track.created_at', 'desc']]): array
    {
        $model = $this->model::query();

        $model = $model->where('contact_id', $contactId);
        if (! empty($startTime)) {
            $model = $model->whereBetween('created_at', [$startTime, $endTime]);
        }
        if ($employeeId > 0) {
            $model->where('employee_id', $employeeId);
        }
        if ($event > 0) {
            $model->where('event', $event);
        }
        if (! empty($orderBy) && is_array($orderBy)) {
            foreach ($orderBy as $v) {
                $model->orderBy($v[0], $v[1]);
            }
        }
        $res = $model->get($columns);
        $res || $res = collect([]);

        if ($res === null) {
            return [];
        }

        return $res->toArray();
    }

    /**
     * 查询多条 - 根据客户ID.
     * @param int $contactId 客户ID
     * @param int $event 事件id
     * @param array|string[] $columns 查询字段
     * @param array $orderBy 排序方式
     * @return array 数组
     */
    public function getContactTracksByContactIdEmployeeIdEventLimit(int $contactId, int $employeeId, string $startTime, string $endTime, int $event = 0, array $columns = ['*'], array $orderBy = [['contact_track.created_at', 'desc']]): array
    {
        $model = $this->model::query();

        $model = $model->where('contact_id', $contactId);
        if ($employeeId > 0) {
            $model->where('employee_id', $employeeId);
        }
        $res = $model->orderByDesc('id')
            ->limit(5)
            ->get($columns);
        $res || $res = collect([]);

        if ($res === null) {
            return [];
        }

        return $res->toArray();
    }
}
